package com.wsc.wsckeeper.managers;

import android.database.sqlite.SQLiteDatabase;

import com.wsc.wsckeeper.app.KApplication;
import com.wsc.wsckeeper.persistence.DatabaseHelper;

public class DatabaseManager extends BaseManager {

	private static DatabaseManager instance;
	private DatabaseHelper helper;
	private SQLiteDatabase database;

	private DatabaseManager() {
	}

	public static DatabaseManager getInstance() {
		if (instance == null) {
			instance = new DatabaseManager();
		}
		return instance;
	}

	public DatabaseHelper getDbHelper() {
		return helper;
	}

	@Override
	public void onCreationComplete() {
		helper = new DatabaseHelper(KApplication.getContext());
		database = helper.getWritableDatabase();
	}

	public void close() {
		database.close();
	}

	public void reopen() {
		close();
		helper = new DatabaseHelper(KApplication.getContext());
		database = helper.getWritableDatabase();
	}

	@Override
	public void onRefresh() {
		onDestroy();
		onCreationComplete();
	}

	@Override
	public void onDestroy() {
		if (helper != null) {
			helper.close();
		}
	}

}
